//
//  SESyndromeController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/7.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SESyndromeController.h"
#import "SESyndromeDTO.h"
#import "SESyndromeCell.h"
@interface SESyndromeController ()

@property (nonatomic, strong) NSArray <SESyndromeDTO *>*dataArray;

@end

@implementation SESyndromeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStyleDone target:self action:@selector(didClickSureButton)];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    [self.tableView registerClass:[SESyndromeCell class] forCellReuseIdentifier:@"testCell"];
}

#pragma mark - 私有方法
//点击确认按钮执行的方法
- (void)didClickSureButton{
    //获取数据数组中被点击的模型 加入到一个可变数组中 然后block传出去
    NSMutableArray *mArray = [NSMutableArray array];
    
    [self.dataArray enumerateObjectsUsingBlock:^(SESyndromeDTO * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj.selected) {
            [mArray addObject:obj];
        }
    }];
    
    if (self.syndromeBlock) {
        self.syndromeBlock(mArray.copy);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - 数据源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SESyndromeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];
    
    SESyndromeDTO *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    cell.cellBlock = ^{
        
        model.selected = !model.isSelected;
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}


#pragma mark - lazyload
- (NSArray<SESyndromeDTO *> *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [SESyndromeDTO modelArray];
    }
    return _dataArray;
}





@end










